<?php
class Seo extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Mseo"));
        $this->breadcrumbs->push('Seo', 'admincp/seo/view');
	}
	
	public function view(){
		if($this->input->is_ajax_request()){
            $start = $this->input->post('iDisplayStart');
            $length = $this->input->post('iDisplayLength');

            $sql = "SELECT SQL_CALC_FOUND_ROWS type, id, url, keywords, description from 
            (
            	SELECT 'page' as type, id, CONCAT('".base_url()."',url_routes,'".$this->config->item('url_suffix')."') as url, keywords, description FROM tpt_page WHERE home_menu = 1
            	UNION ALL
            	SELECT 'article' as type, a.id as id, CONCAT('".base_url()."',d.url_routes,'/',a.id,'-',b.url,'".$this->config->item('url_suffix')."') as url, a.keywords, a.description FROM tpt_article AS a 
            		LEFT JOIN tpt_article_content AS b ON a.id = b.art_id AND b.lang_id = 2
            		LEFT JOIN tpt_catalogue AS c ON a.cata_id = c.id
					LEFT JOIN tpt_page AS d ON c.id = d.cata_id
				WHERE
					a.check = 1
				GROUP BY 
					a.id,d.cata_id
            ) AS a
			LIMIT $start, $length";

            $data['list_seo'] = $this->db->query($sql)->result_array();
            
            $sql = "SELECT FOUND_ROWS() AS total";
            $rows = $this->db->query($sql)->row_array();

            if ((isset($data['list_seo']) == TRUE) && ($data['list_seo'] != '') && (is_array($data['list_seo']) == TRUE)) {
                $aaData = array();                
                foreach ($data['list_seo'] as $key => $value) {
                    $aaData[$key][] = '<td>' . $value['type'] . '</td>';
                    $aaData[$key][] = '<td>' . anchor($value['url'],$value['url'],array('target' => '_blank')) . '</td>';
                    $aaData[$key][] = '<td>' . $value['keywords'] . '</td>';
                    $aaData[$key][] = '<td>' . $value['description'] . '</td>';
                    $aaData[$key][] = '<td> <a href="' . admincp_url() . $value['type'].'/edit/id/' . $value['id'] . '" class="btn btn-default tip" title="Edit"><i class="fa '.$this->config->item('button_edit').'"></i></a></td>';
                }
                $result = array(
                	"aaData" => $aaData,
                	"iTotalDisplayRecords" => $rows['total']
            	);
            }

            $this->output->set_output(json_encode($result));
        }else{
            $data['icon_page']=$this->config->item('icon_view');
            $data['title']='Seo Manager';
            $data['sub_title']='Seo Manager';
			$this->parser->parse("seo/view.tpl", NULL, $data);
        }
	}
}